import pandas as pd
import matplotlib.pyplot as plt
from io import StringIO


data = """Date\tDMI

"""


df = pd.read_csv(StringIO(data), sep="\t")
df['Date'] = pd.to_datetime(df['Date'])
df['YearMonth'] = df['Date'].dt.strftime('%Y-%m')


def classify_dmi(dmi):
    if dmi >= 0.4:
        return '正IOD'
    elif dmi <= -0.4:
        return '负IOD'
    else:
        return '中性IOD'

df['IOD_Type'] = df['DMI'].apply(classify_dmi)


landslide_months = ['1961-07', '1961-08', '1964-09', '1970-08', '1972-07', '1972-08', '1973-07']


iod_counts = {'正IOD': 0, '负IOD': 0, '中性IOD': 0}

for month in landslide_months:
    row = df[df['YearMonth'] == month]
    if not row.empty:
        iod_type = row.iloc[0]['IOD_Type']
        iod_counts[iod_type] += 1


labels = iod_counts.keys()
sizes = iod_counts.values()
colors = ['tomato', 'skyblue', 'lightgreen']

plt.figure(figsize=(6, 6))
plt.pie(sizes, labels=labels, autopct='%1.1f%%', colors=colors, startangle=90)
plt.title('图3.2 正IOD、负IOD与中性期泥石流发生比例分布')
plt.axis('equal')
plt.show()
